package com.ymttest.business.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymt.base.YmatouBaseCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RemoteCmd;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.database.sqlwapper.BetterWapper;
import com.ymttest.utils.TestDataManager;

public class Data201712 {

	public static void main(String[] args) throws Exception {
		// betterdomain来源
		// better 捞数据sql
		// --- 站点最后一次发布记录为180天前
		// SELECT
		// a.*,b.domain,b.`name`,b.devs
		// FROM
		// (
		// SELECT
		// appid,
		// max(time) AS buildTime
		// FROM
		// BuildHistory
		// GROUP BY
		// appid
		// ) a , Application b
		// WHERE a.appid=b.id and
		// a.buildTime < DATE_ADD(now(), INTERVAL - 180 DAY)
		// ORDER BY
		// buildTime DESC

		// 查询cmdb中不存在的站点
		// String
		// url="http://portal.ymatou.cn/cmdb/applications/applicationgroup/?d=1513331496664&page=1&page_size=10&search=";
		// ArrayList<String> list =
		// TestDataManager.getLines("betterdomain.txt");
		// for (String string : list) {
		// YmatouEasyCall ec=new YmatouEasyCall(url+string, "GET", "PARAM");
		// ec.addHeader("Cookie", "csrftoken=WoXPU0Md9l0a43wOQ1sKileHFMdbqaFL;
		// sessionid=y13s4rtk8px4nv3xqnl4sbo0xowj4h0r;
		// messages=\"18fd5c45d603e870b2d4c2fbfe3f9bc8401119dc$[[\\\"__json_message\\\"\\0540\\05425\\054\\\"Login
		// succeeded. Welcome\\054
		// zhouyi.\\\"]\\054[\\\"__json_message\\\"\\0540\\05425\\054\\\"Login
		// succeeded. Welcome\\054 zhouyi.\\\"]]\"\");");
		// ec.callService();
		// int
		// count=ec.getJsonObject().get("stdout").getAsJsonObject().get("count").getAsInt();
		// if(count==0){
		// TestDataManager.appendFileToLine("domain.txt", string);
		// }
		// }
		// 格式化数据根据ip来确认站点
		BetterWapper better = new BetterWapper();
		ArrayList<String> list = TestDataManager.getLines("domain.txt");
		Map<String, List> m = new TreeMap<String, List>();
		for (String domain : list) {
			List<Map> mlist = better.selectApplicationIpByDomain(domain);
			for (Map map : mlist) {
				String ip = map.get("ip").toString();
				List domainlist = m.get(ip);
				if (domainlist == null) {
					domainlist = new ArrayList<>();
				}
				domainlist.add(domain);
				Collections.sort(domainlist);
				m.put(ip, domainlist);
			}
		}
		Logger.debug(FormatUtil.beanToGSONString(m));
		// C:\Windows\winsxs\wow64_microsoft-windows-iis-sharedlibraries_31bf3856ad364e35_6.1.7600.16385_none_77330ebe02e41fee
		// set
		// path=%path%;C:\Windows\winsxs\wow64_microsoft-windows-iis-sharedlibraries_31bf3856ad364e35_6.1.7600.16385_none_77330ebe02e41fee
		// 删除站点dns
		// ArrayList<String> list = TestDataManager.getLines("temp.txt");
		// for (String domain : list) {
		// if (domain.indexOf(".ymatou") > 0) {
		// String secenddm = domain.substring(0, domain.indexOf(".ymatou"));
		// String rdm = domain.substring(domain.indexOf(".ymatou")+1);
		// String [] ss=new String[]{"dnscmd . /RecordDelete "+rdm+"
		// "+secenddm+" A /F"};
		// RemoteCmd.exec("172.16.110.11", "administrator", "ymtcsnb", ss);
		// }
		// }
//		List<String> list33 = m.get("172.16.101.38");
//		for (String s : list33) {
//			String[] ss = new String[] { "sc stop " + s };
//			RemoteCmd.exec("172.16.101.38", "administrator", "1234qwer", ss);
//			ss = new String[] { "sc delete " + s };
//			RemoteCmd.exec("172.16.101.38", "administrator", "1234qwer", ss);
//		}

	}
}
